class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        int len = nums.size();
        if (len == 0) {
            return 0;
        }
        vector<int> hash(10001);
        for (auto e : nums) {
            hash[e] += e;
        }
        int len2 = hash.size();
        if (len2 == 0) {
            return 0;
        }
        vector<int> f(len2);
        vector<int> g(len2);
        f[0] = hash[0];
        for (int i = 1; i < len2; i++) {
            f[i] = g[i - 1] + hash[i];
            g[i] = max(f[i - 1], g[i - 1]);
        }
        return max(f[len2 - 1], g[len2 - 1]);
    }
};